using System.Collections;
using UnityEngine;

public class GGLeaderboardsPopulation : MonoBehaviour
{
	public delegate void OnComplete();

	public int level;

	public int startPid;

	public int endPid;

	public string nameBase;

	public int startScore;

	public string updateCountry = "czech";

	private int currentPid = -1;

	private OnComplete onPopulationComplete;

	public void UpdateLeaderboards(OnComplete onComplete)
	{
		onPopulationComplete = onComplete;
		currentPid = ((currentPid < 0) ? startPid : (currentPid + 1));
		GGServerRequestsBackend.UpdateRequest updateRequest = new GGServerRequestsBackend.UpdateRequest(level, nameBase + currentPid, updateCountry.ToLower());
		int num = startScore;
		updateRequest.SetScore(num);
		updateRequest.SetPid(currentPid.ToString());
		Debug.Log("pid: " + currentPid + ", name: " + nameBase + ", level: " + level + ", score: " + num);
		StartCoroutine(DoUpdateUser(updateRequest, onRequestComplete));
	}

	private IEnumerator DoUpdateUser(GGServerRequestsBackend.UpdateRequest update, GGServerRequestsBackend.OnComplete onComplete)
	{
		GGServerRequestsBackend.NonceRequest nonceReq = new GGServerRequestsBackend.NonceRequest();
		yield return StartCoroutine(nonceReq.RequestCoroutine());
		if (nonceReq.status == GGServerRequestsBackend.ServerRequest.RequestStatus.Success)
		{
			update.nonce = nonceReq.GetResponse<string>();
			Debug.Log("Nonce: " + update.nonce);
			yield return StartCoroutine(update.RequestCoroutine());
		}
		Debug.Log("Update success " + update.status);
		if (onComplete != null)
		{
			onComplete(update);
		}
	}

	public void onRequestComplete(GGServerRequestsBackend.ServerRequest request)
	{
		if (currentPid < endPid)
		{
			UpdateLeaderboards(onPopulationComplete);
			return;
		}
		currentPid = -1;
		if (onPopulationComplete != null)
		{
			onPopulationComplete();
			onPopulationComplete = null;
		}
	}
}
